<?php
class controleur {
	private $cpdo;
	private $extra;
	public function __construct() {
		session_start ();
		$this->cpdo = new MyPDO ( 'mysql:host=mysql1.alwaysdata.com;dbname=rdelaporte_ppeusep', '90305_robin', 'squateur', array(\PDO::MYSQL_ATTR_INIT_COMMAND =>  'SET NAMES utf8') );
	}
	public function __set($propriete, $valeur) {
		switch ($propriete) {
			case 'style' :
				{
					$this->style [count ( $this->style ) + 1] = $valeur;
					break;
				}
					
			case 'corps' :
				{
					$this->corps = $valeur;
					break;
				}
			case 'titre' :
				{
					$this->titre = $valeur;
					break;
				}
		}
	}
	public function GetConnectBD() {
		return $this->cpdo;
	}
	public function creasession($nom) { // AJOUTER L'ID DE L'ECOLE EN VARIABLE SESSION
		if ($nom == "admin") {
			$_SESSION ['admin'] = $nom;
		} else {
			$_SESSION ['pseudo'] = $nom;
		}
	}
	public function deconnexion() {
		$_SESSION = array ();
		session_destroy ();
		header ( 'location:index.php' );
	}
	public function affiche_menu() {
		$requete = new PreparationREQ ( $this->cpdo );
		if (isset ( $_SESSION ['pseudo'] ) || isset ( $_SESSION ['admin'] )) {
				
			if (isset ( $_SESSION ['admin'] )) {
				$menu = $requete->SelectMenu ( 'menu_admin' );
				return utf8_encode($menu->menu);
			} else {
				$menu = $requete->SelectMenu ( 'menu_registered' );
				return $menu->menu;
			}
		} else {
			$menu = $requete->SelectMenu ( 'menu_unregistered' );
			return $menu->menu;
		}
	}
	public function InscriptionSite() {
		if (isset ( $_POST ['nomEcole'] ) && isset ( $_POST ['villeEcole'] ) && isset ( $_POST ['loginEcole'] ) && isset ( $_POST ['passwordEcole'] ) && isset ( $_POST ['usermail'] ) && isset ( $_POST ['usep'] )) {
				
			$requete = new PreparationREQ ( $this->cpdo );
				
			$validate = $this->CheckDatabase ();
			if ($validate == 'Ok') {
				$inscription = $requete->InsertEcole ( $_POST ['nomEcole'], $_POST ['villeEcole'], $_POST ['loginEcole'], $_POST ['passwordEcole'], $_POST ['usermail'], $_POST ['usep'] );
				if ($inscription >= 1) {
					$mailsent= $this->sendMail($_POST ['usermail'],$_POST ['nomEcole'],$_POST ['loginEcole'],$_POST ['passwordEcole']);
					if($mailsent){
						echo'un email de confirmation vous a été envoyé<br>';
					}
					echo 'vous êtes bien inscrit<br>';
				}
			} else {
				echo $validate;
			}
		}
	}
	public function InscriptionCourse() {
	}
	public function CheckDatabase() {
		$VRETOUR = 'Ok';
		$validate = true;
		$requete = new PreparationREQ ( $this->cpdo );
		$req = $requete->SelectAllEcole ();
		foreach ( $req as $row ) {
			if ($row ["nomecole"] == $_POST ['nomEcole'] || $row ["ville"] == $_POST ['villeEcole'] || $row ["mail"] == $_POST ['usermail'] || $row ["login"] == $_POST ['loginEcole']) {
				if ($row["nomecole"] == $_POST ['nomEcole'] && $row ["ville"] == $_POST ['villeEcole']) {
					$VRETOUR = 'Cette école éxiste déja pour cette ville';
				}
				if($row["mail"] == $_POST ['usermail']){
					$VRETOUR = '<br>Cette adresse 	mail est déjà utilisée.';
				}
				if ($row ["login"] == $_POST ['loginEcole']) {
					$VRETOUR = $VRETOUR . '<br>Ce login est déja utilisé.';
				}
			}
		}
		return $VRETOUR;
	}
	private function sendMail($mail,$name,$login,$password){
		$to = $mail;
		$subject = "Confirmation d'incription";
		$message = "<b>Do not reply.</b>";
		$message .= "<h1>Confirmation de votre inscription:.</h1>";	
		$message .= "<table>
  <tr>
    <td>Nom de l'école</td>
    <td>Login</td> 
    <td>Mot de passe</td>
  </tr>
  <tr>
    <td>".$name."</td>
    <td>".$login."</td> 
    <td>".$password."</td>
  </tr>
</table><br><a href='http://localhost/ppeusep/'>Cliquez ici pour vous connecter à votre compte</a>";
		$header = "From:robin-delaporte@hotmail.fr \r\n";
		$header .= "MIME-Version: 1.0\r\n";
		$header .= "Content-type: text/html\r\n";
		return mail ($to,$subject,$message,$header);
		
	
	}
}

?>